package mydb

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	user     = "root"
	pass     = "123456"
	ip       = "47.115.223.32"
	port     = "3306"
	database = "web"
)

func GetDB() (*gorm.DB, error) {

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, pass, ip, port, database)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		//SkipDefaultTransaction: true,
		//NamingStrategy: schema.NamingStrategy{
		//	TablePrefix:   "t_",
		//	SingularTable: true,
		//	NoLowerCase:   false,
		//},
		//Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic("数据库连接失败" + err.Error())
	}
	return db, err
}

func AddOne(db *gorm.DB, entry interface{}) error {
	err := db.Create(entry).Error
	return err
}

func AddMany(db *gorm.DB, entry interface{}) error {
	err := db.Create(entry).Error
	return err
}
